<?php 
/**
 * This file is part of DomAr.
 * 
 * DomAr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomAr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomAr.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @package DomAr
* @subpackage Parser
*/
class DomArClassParserRecordProperty_Number extends DomArClassParserRecordProperty {
	
	protected $length = 11;
	
	protected $decimals = 0;
	
	protected $in = array();

	protected $unsigned = false;
	
	protected $null = true;
	
	protected $index = NULL;
	
	protected function setOrm( array &$orm ) {
		
		$this->length = (int)$orm['length'][0];
		$this->decimals = (int)$orm['length'][1];
		
		$this->in = (array)$orm['inArgs'];

		// convert arguments to their propert formats.
		foreach( $this->in as $i => &$arg ) {
			if( strpos($arg,'-') > 0 ) { 
				$arg = explode('-',$arg);
				$arg = array( 0 => (float)$arg[0], 1 => (float)$arg[1]);
			} else {
				$arg = (float)$arg;
			}
			if( $this->decimals <= 0 ) {
				if( is_array($arg) ) {
					$arg[0] = (int)$arg[0];
					$arg[1] = (int)$arg[1];
				} else {
					$arg = (int)$arg;
				}		
			}
		}
		
		$this->null = !$orm['notNull'];
		$this->index = $orm['index'];
		$this->unsigned  = (bool)$orm['unsigned'];
	}
	
	protected function updateHandlerClassName( DomArClassParser $class ) {
		$this->handlerClassName = 'DomArProperty_Number';
	}
	
	protected function updateCreateHandlerCode( DomArClassParser $class ) {
		$in = '';
		foreach( $this->in as $var )
			$in .= (is_array($var) === true ? 'array('.$var[0].','.$var[1].')' : $var).',';
		
		$this->createHandlerCode =  'new DomArProperty_Number(\''.$this->name.'\','.$this->length.','.$this->decimals.','.($this->null ? 'true' : 'false').','.($this->unsigned ? 'true' : 'false').',\''.$this->index.'\',array('.$in.'))';
	}

}

?>